package service;

import java.util.List;
import java.util.Map;

import com.opensymphony.xwork2.ActionContext;

import action.InviteAction;
import dao.InviteDao;
import po.Order;
import po.OrderWithUser;

public class InviteService {
	private List<Order> list;
	private Map<String, Object> request, session;
	//发布的家教信息查询
	public Boolean searchOrder(String type,String course,String sex) {
		String hql = "from Order as user where type=" + "'"
						+type+"' and course='"+course+"' and sex='"+sex+"' and status=0";
		//多表查询
		//String hql = "from Order a,User b" + " where a.promulgatorID = b.customerId and a.type = '"+type+"' and a.course = '"+course+"' and b.userDetail.sex = '" + sex +"'";
		InviteDao dao = new InviteDao();
		list = dao.findByHql(hql);
		if(list.isEmpty()) {
			return false;
		}
		
		//System.out.println(list.size());
		return true;
	}

	//查询所有发布的家教信息，并统计部分信息
	public Boolean getAllOrder() {
		ActionContext ctx= ActionContext.getContext();
		session=(Map) ctx.getSession();
		request=(Map) ctx.get("request");
		String hql = "from Order as user where status=0";
		InviteDao dao = new InviteDao();
		list = dao.findByHql(hql);
		if(list.isEmpty()) {
			return false;
		}
		//由返回数组得到部分统计信息---不完整
		int number = list.size(); //当前课程总数
		int math = 0;
		int english = 0;
		int china = 0;
		for(int i=0;i<list.size();i++) {
			if(list.get(i).getCourse().equals("数学")) {
				math++;
			}
			if(list.get(i).getCourse().equals("语文")) {
				china++;
			}
			if(list.get(i).getCourse().equals("英语")) {
				english++;
			}
		}
		request.put("tip", "课程统计信息："+number+"门 数学课程："+math+"门 语文课程："+china+"门 英语课程："+english+"门");
		return true;
	}

	public List getOrders() {
		return list;
	}
}
